# -*- coding: utf-8 -*-
import scrapy
from ..items import StoryItem


class StorySpider(scrapy.Spider):
    name = 'story'
    allowed_domains = ['biqudao.cc']

    def start_requests(self):
        # for i in range(1, 73):
        #     yield scrapy.Request('https://m.biqudao.cc/26/26558_' + str(i) + '/', self.parse)
        yield scrapy.Request('https://m.biqudao.cc/26/26558_73', self.parse)

    def parse(self, response):
        title = response.xpath("//h1/a/text()").get()
        for item in response.xpath("//ul/li"):
            yield scrapy.Request(item.xpath("./a/@href").get(), callback=self.detail_parse, meta={'title': title})

    def detail_parse(self, response):
        # 获取章节名
        section_title = response.xpath("//h1[@id='chaptertitle']/text()").get()
        part_one = response.xpath("string(//div[@id='novelcontent']/p)").get()
        second_page_link = response.xpath("//a[@id='pb_next']/@href").get()
        yield scrapy.Request('https://m.biqudao.cc' + second_page_link, callback=self.second_page_parse,
                             meta={'part_one': part_one, 'section_title': section_title})

    def second_page_parse(self, response):
        item = StoryItem()
        item['section_title'] = response.meta['section_title']
        item['content'] = response.meta['part_one'] + response.xpath("string(//div[@id='novelcontent']/p)").get()
        yield item
